import datetime
import time
from time import strftime

from flask import Blueprint, render_template, request

from applications.common.curd import model_to_dicts, enable_status, disable_status
from applications.common.helper import ModelFilter
from applications.common.utils.http import table_api, fail_api, success_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import xss_escape
from applications.extensions import db

from applications.models import Timetable
from applications.schemas import TimetableOutSchema

admin_timetable_homepage = Blueprint('admintimetableHomePage', __name__, url_prefix='/admin/daliy/timetable')
l = []
l1 = []
l2 = []


@admin_timetable_homepage.get('/')
def main():
    mon = Timetable.query.filter_by(weekday='星期一').first()
    Mon = Timetable(weekday=mon.weekday, mor_exercise=mon.mor_exercise, morning_1=mon.morning_1, morning_2=mon.morning_2,\
                        afternoon_1=mon.afternoon_1, \
                        afternoon_2=mon.afternoon_2, evening_exercise=mon.evening_exercise, charger=mon.charger)

    tue = Timetable.query.filter_by(weekday='星期二').first()
    Tue = Timetable(weekday=tue.weekday, mor_exercise=tue.mor_exercise, morning_1=tue.morning_1,
                    morning_2=tue.morning_2, \
                    afternoon_1=tue.afternoon_1, \
                    afternoon_2=tue.afternoon_2, evening_exercise=tue.evening_exercise, charger=tue.charger)

    wed = Timetable.query.filter_by(weekday='星期三').first()
    Wed = Timetable(weekday=wed.weekday, mor_exercise=wed.mor_exercise, morning_1=wed.morning_1,
                    morning_2=wed.morning_2, \
                    afternoon_1=wed.afternoon_1, \
                    afternoon_2=wed.afternoon_2, evening_exercise=wed.evening_exercise, charger=wed.charger)
    thu = Timetable.query.filter_by(weekday='星期四').first()
    Thu = Timetable(weekday=thu.weekday, mor_exercise=thu.mor_exercise, morning_1=thu.morning_1,
                    morning_2=thu.morning_2, \
                    afternoon_1=thu.afternoon_1, \
                    afternoon_2=thu.afternoon_2, evening_exercise=thu.evening_exercise, charger=thu.charger)

    fri = Timetable.query.filter_by(weekday='星期五').first()
    Fri = Timetable(weekday=fri.weekday, mor_exercise=fri.mor_exercise, morning_1=fri.morning_1,
                    morning_2=fri.morning_2, \
                    afternoon_1=fri.afternoon_1, \
                    afternoon_2=fri.afternoon_2, evening_exercise=fri.evening_exercise, charger=fri.charger)

    sat = Timetable.query.filter_by(weekday='星期六').first()
    Sat = Timetable(weekday=sat.weekday, mor_exercise=sat.mor_exercise, morning_1=sat.morning_1,
                    morning_2=sat.morning_2, \
                    afternoon_1=sat.afternoon_1, \
                    afternoon_2=sat.afternoon_2, evening_exercise=sat.evening_exercise, charger=sat.charger)

    sun = Timetable.query.filter_by(weekday='星期天').first()
    Sun = Timetable(weekday=sun.weekday, mor_exercise=sun.mor_exercise, morning_1=sun.morning_1,
                    morning_2=sun.morning_2, \
                    afternoon_1=sun.afternoon_1, \
                    afternoon_2=sun.afternoon_2, evening_exercise=sun.evening_exercise, charger=sun.charger)

    return render_template('admin/daliy/timetable.html',
                           Mon=Mon,
                           Tue=Tue,
                           Wed=Wed,
                           Thu=Thu,
                           Fri=Fri,
                           Sat=Sat,
                           Sun=Sun)


#   分页查询
@admin_timetable_homepage.get('/data')
def data():
    # 获取请求参数
    week = xss_escape(request.args.get('weekday', type=str))
    Datet = xss_escape(request.args.get('day', type=datetime))
    morexercise = xss_escape(request.args.get('mor_exercise', type=str))
    morning1 = xss_escape(request.args.get('morning_1', type=str))
    morning2 = xss_escape(request.args.get('morning_2', type=str))
    afternoon1 = xss_escape(request.args.get('afternoon_1', type=str))
    afternoon2 = xss_escape(request.args.get('afternoon_2', type=str))
    eveningexercise = xss_escape(request.args.get('evening_exercise', type=str))
    charger = xss_escape(request.args.get('charger', type=str))
    # 查询参数构造
    mf = ModelFilter()
    if week:
        mf.contains(field_name="weekday", value=week)
    if Datet:
        mf.contains(field_name="day", value=Datet)
    if morexercise:
        mf.contains(field_name="mor_exercise", value=morexercise)
    if morning1:
        mf.contains(field_name="morning_1", value=morning1)
    if morning2:
        mf.contains(field_name="morning_2", value=morning2)
    if afternoon1:
        mf.contains(field_name="afternoon_1", value=afternoon1)
    if afternoon2:
        mf.contains(field_name="afternoon_2", value=afternoon2)
    if eveningexercise:
        mf.contains(field_name="evening_exercise", value=eveningexercise)
    if charger:
        mf.contains(field_name="charger", value=charger)
    # mf.inlist(field_name="id", value=l)

    # orm查询
    # 使用分页获取data需要.items
    timetable = Timetable.query.filter(mf.get_filter(model=Timetable)).layui_paginate()
    count = timetable.total
    # 返回api
    return table_api(data=model_to_dicts(schema=TimetableOutSchema, data=timetable.items), count=count)


# 修改当前用户信息
@admin_timetable_homepage.put('/updateInfo')
def update_info():
    req_json = request.json
    r1 = Timetable.query.filter_by(weekday='星期一').update(
        {"mor_exercise": req_json.get("mor_exercise1"),"morning_1": req_json.get("morning_11"),\
         "morning_2": req_json.get("morning_21"),"afternoon_1": req_json.get("afternoon_11"),\
         "afternoon_2": req_json.get("afternoon_21"),"evening_exercise": req_json.get("evening_exercise1"),\
         "charger": req_json.get("charger1")})
    r2 = Timetable.query.filter_by(weekday='星期二').update(
        {"mor_exercise": req_json.get("mor_exercise2"), "morning_1": req_json.get("morning_12"), \
         "morning_2": req_json.get("morning_22"), "afternoon_1": req_json.get("afternoon_12"), \
         "afternoon_2": req_json.get("afternoon_22"), "evening_exercise": req_json.get("evening_exercise2"), \
         "charger": req_json.get("charger2")})
    r3 = Timetable.query.filter_by(weekday='星期三').update(
        {"mor_exercise": req_json.get("mor_exercise3"), "morning_1": req_json.get("morning_13"), \
         "morning_2": req_json.get("morning_23"), "afternoon_1": req_json.get("afternoon_13"), \
         "afternoon_2": req_json.get("afternoon_23"), "evening_exercise": req_json.get("evening_exercise3"), \
         "charger": req_json.get("charger3")})
    r4 = Timetable.query.filter_by(weekday='星期四').update(
        {"mor_exercise": req_json.get("mor_exercise4"), "morning_1": req_json.get("morning_14"), \
         "morning_2": req_json.get("morning_24"), "afternoon_1": req_json.get("afternoon_14"), \
         "afternoon_2": req_json.get("afternoon_24"), "evening_exercise": req_json.get("evening_exercise4"), \
         "charger": req_json.get("charger4")})
    r5 = Timetable.query.filter_by(weekday='星期五').update(
        {"mor_exercise": req_json.get("mor_exercise5"), "morning_1": req_json.get("morning_15"), \
         "morning_2": req_json.get("morning_25"), "afternoon_1": req_json.get("afternoon_15"), \
         "afternoon_2": req_json.get("afternoon_25"), "evening_exercise": req_json.get("evening_exercise5"), \
         "charger": req_json.get("charger5")})
    r6 = Timetable.query.filter_by(weekday='星期六').update(
        {"mor_exercise": req_json.get("mor_exercise6"), "morning_1": req_json.get("morning_16"), \
         "morning_2": req_json.get("morning_26"), "afternoon_1": req_json.get("afternoon_16"), \
         "afternoon_2": req_json.get("afternoon_26"), "evening_exercise": req_json.get("evening_exercise6"), \
         "charger": req_json.get("charger6")})
    r7 = Timetable.query.filter_by(weekday='星期天').update(
        {"mor_exercise": req_json.get("mor_exercise7"), "morning_1": req_json.get("morning_17"), \
         "morning_2": req_json.get("morning_27"), "afternoon_1": req_json.get("afternoon_17"), \
         "afternoon_2": req_json.get("afternoon_27"), "evening_exercise": req_json.get("evening_exercise7"), \
         "charger": req_json.get("charger7")})
    db.session.commit()
    if not r1:
        return fail_api(msg="出错啦")
    return success_api(msg="更新成功")